package cn.funnymap.lgis.file;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wenzf
 * @since 2025/8/25 10:31
 */
public enum Unit {
	BYTE(0, "B"),
	KB(1, "KB"),
	MB(2, "MB"),
	GB(3, "GB"),
	TB(4, "TB"),
	PB(5, "PB"),
	EB(6, "EB"),;
	
	private static final Map<Integer, Unit> codeToEnumMap = new HashMap<>();
	private static final Map<String, Unit> nameToEnumMap = new HashMap<>();
	static {
		// 将每个枚举的 code 映射到对应的枚举实例
		for (Unit status : Unit.values()) {
			codeToEnumMap.put(status.code, status);
			nameToEnumMap.put(status.name, status);
		}
	}
	@EnumValue
	@Getter
	private final int code;
	@JsonValue
	@Getter
	private final String name;
	Unit(int code, String name) {
		this.code = code;
		this.name = name;
	}
	public static Unit fromCode(Integer code) {
		return codeToEnumMap.get(code);
	}
	@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
	public static Unit fromName(String name) {
		return nameToEnumMap.get(name);
	}
}
